import * as types from './actionTypes';
import reqwest from 'reqwest';

import Request from '../../utils/request.js';
import API from '../../../../config/api';
import {message} from 'antd';
export function setToolActiveIndex(index,data){
    return {type:types.SET_ACTIVE_TOOL_INDEX,index,data}
}
export function changeTabKey(key){
    if(key != "home") key = parseInt(key);
    return {type:types.CHANGE_TAB_KEY,key}
}
export function setRows(rows){
    return {type:types.SET_ROWS,rows}
}
export function delModule(index){
    return {type:types.DEL_MODULE,index}
}
export function addModule(index){
    return {type:types.ADD_MODULE,index}
}
export function uploadSwiperPic(data){
    return (dispatch,getState) => {
        Request.uploadPic(
            data,
            response=>{
                let { url } = response.body;
                let {row,selectSwiperIndex} = getState().page;
                row = row.setIn(['swiperData',"state","picItems",selectSwiperIndex,'url'], url);
                //console.log(selectSwiperIndex,row);
                dispatch(setRow(row));
            },
            (err,msg)=>{

            }
        );
    }
}
export function uploadSwiperSubPic(data,subIndex){
    return (dispatch,getState) => {
        Request.uploadPic(
            data,
            response=>{
                let { url } = response.body;
                let {row,selectSwiperIndex} = getState().page;
                console.log(selectSwiperIndex);
                row = row.setIn(['swiperData',"state","picItems",selectSwiperIndex,'items',subIndex,"url"], url);
                //console.log(index,row);
                dispatch(setRow(row));
            },
            (err,msg)=>{

            }
        );
    }
}

export function uploadBtnSubPic(data,subIndex){
    return (dispatch,getState) => {
        Request.uploadPic(
            data,
            response=>{
                let { url } = response.body;
                let {row,selectBtnIndex} = getState().page;
                row = row.setIn(['diyData',"state","diyRows",selectBtnIndex,'items',subIndex,"pic"], url);
                //console.log(index,row);
                dispatch(setRow(row));
            },
            (err,msg)=>{

            }
        );
    }
}
export function uploadBtnPic(data){
    return (dispatch,getState) => {
        Request.uploadPic(
            data,
            response=>{
                let { url } = response.body;
                let {row,selectBtnIndex} = getState().page;
                row = row.setIn(['diyData',"state","diyRows",selectBtnIndex,'bg'], url);
                //console.log(index,row);
                dispatch(setRow(row));
            },
            (err,msg)=>{

            }
        );
    }
}
export function uploadBottomPic(data){
    return (dispatch,getState) => {
        Request.uploadPic(
            data,
            response=>{
                let { url } = response.body;
                let {row} = getState().page;
                row = row.setIn(['bottomPic',"url"], url);
                dispatch(setRow(row));
            },
            (err,msg)=>{

            }
        );
    }
}
export function setRow(row){
    return {type:types.SET_ROW,row}
}
export function setDetail(row){
    return {type:types.SET_DETAIL,row}
}

export function fetchingDetail(isDo = true){
    return {type:types.FETCHING_DETAIL,isDo}
}

export function getRows(){
    return (dispatch)=>{
        reqwest(Request.getOptions(
            "post",
            API.mobileAdminPages,
            null,
            true
        )).then( response => {
            dispatch(setRows(response))
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{
            //dispatch(setLoading(false));
        })
    }
}
export function getRow(hash){
    return (dispatch)=>{
        dispatch(fetchingDetail(true));
        reqwest(Request.getOptions(
            "get",
            API.mobileAdminPage+"/"+hash,
            null,
            true
        )).then( response => {
            dispatch(setDetail(response))
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{
            dispatch(fetchingDetail(false));
        });
    }
}
export function deleteRow(hash){
    return (dispatch)=>{
        reqwest(Request.getOptions(
            "delete",
            API.mobileAdminPage+"/"+hash,
            null,
            true
        )).then( response => {
            message.success(response.message);
            dispatch(getRows({}))
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{
            //dispatch(setLoading(false));
        })
    }
}

export function saveRow(){
    return (dispatch,getState)=>{
        let row = getState().page.detail;
        reqwest(Request.getOptions(
            "put",
            API.mobileAdminPage+"/"+row.hash,
            row,
            true
        )).then( response => {
            message.success("保存成功");
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{
            //dispatch(setLoading(false));
        });
    }
}

export function addRow(title){
    return (dispatch)=>{
        reqwest(Request.getOptions(
            "post",
            API.mobileAdminPage,
            {title},
            true
        )).then( response => {
            message.success(response.message);
            dispatch(getRows({}))
        }).fail(function (err) {
            Request.handleError(err);
        }).always((resp) =>{
            //dispatch(setLoading(false));
        })
    }
}